package com.hyyt.shopjsp.product.controller;

import com.hyyt.shopjsp.basic.pojo.BasicUsers;
import com.hyyt.shopjsp.basic.service.IBasicActorPurviewService;
import com.hyyt.shopjsp.common.controller.BaseController;
import com.hyyt.shopjsp.product.pojo.*;
import com.hyyt.shopjsp.product.service.IShopProductAttributeValueService;
import com.hyyt.shopjsp.product.service.IShopProductAttributeService;
import com.hyyt.shopjsp.product.service.IShopProductTypeService;
import com.hyyt.shopjsp.redis.service.IRedisInitializingBasicDataService;
import com.hyyt.shopjsp.util.common.Page;
import com.hyyt.shopjsp.util.common.ResultMsg;
import com.hyyt.shopjsp.util.common.Utils;
import com.hyyt.shopjsp.util.json.FastJsonUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @author Administrator on 2017/7/31.
 */
@Controller
@RequestMapping("/back/productAttribute")
public class ShopProductAttributeController extends BaseController{

    private Logger logger = Logger.getLogger(ShopProductAttributeController.class);
    private String basePath = "/back/shop/product/productAttribute/";

    @Resource
    private IShopProductAttributeService shopProductAttributeService;
    @Resource
    private IShopProductAttributeValueService shopAttributeValueService;
    @Resource
    private IRedisInitializingBasicDataService redisInitializingBasicDataService;
    @Resource
    private IShopProductTypeService shopProductTypeService;

    /**
     * 进入商品属性页面
     * @param param
     * @return
     */
    @RequestMapping("gotoProductAttributePage")
    public String gotoProductAttributePage(Map<String, Object> param){
        param.put("productTypeId","16");
        return basePath+"productAttributeList";
    }

    /**
     * 查询商品分类下的商品属性
     * @param page
     * @param   name            属性名称
     * @param productTypeId     商品分类ID
     */
    @RequestMapping(value = "listProductAttribute", method = RequestMethod.POST)
    @ResponseBody
    public void listProductAttribute(String productTypeId,String name,Page page,HttpServletResponse response) {
        Map<String, Object> param = new HashMap<>();
        if(Utils.objectIsEmpty(productTypeId)){
            productTypeId = "16";
        }
        if (Utils.objectIsNotEmpty(name)){
            param.put("name",name.trim());
        }
        param.put("productTypeId",productTypeId);
        int totalRecordCount = shopProductAttributeService.countShopProductAttribute(param);
        page.setPageInfo(page.getPageSize(), totalRecordCount, page.getCurrentPage());
        List<Map<String,Object>> shopProductAttributeList = shopProductAttributeService.queryShopProductAttributeByProductTypeId(param, page.getPageRecordBeginIndex(), page.getPageSize());
        String  productTypeName = redisInitializingBasicDataService.getProductTypeNameById(Integer.parseInt(productTypeId));
        for(Map<String,Object> map:shopProductAttributeList){
            map.put("productTypeName",productTypeName);
        }
        param.clear();
        param.put("total", totalRecordCount);// total键 存放总记录数，必须的
        param.put("rows", shopProductAttributeList);// rows键 存放每页记录 list
        ResultMsg.create(param, response);  //把内容发送到页面
    }

    /**
     * 获取商品属性和属性值
     * @param productAttrId
     * @return
     */
    @RequestMapping(value="getProductAttributeInfo",method= RequestMethod.POST)
    @ResponseBody
    public ResultMsg getProductAttributeInfo(String productAttrId){
        if(Utils.objectIsNotEmpty(productAttrId)){
           ShopProductAttribute shopProductAttribute = shopProductAttributeService.loadShopProductAttribute(Integer.parseInt(productAttrId));
           ShopProductType shopProductType = shopProductTypeService.loadShopProductType(shopProductAttribute.getProductTypeId());
           Map<String,Object> param = new HashMap<>();
           param.put("productAttrId",productAttrId);
           int attributeValueCount = shopAttributeValueService.countShopAttributeValue(param);
           List<ShopProductAttributeValue> shopProductAttributeValueList = shopAttributeValueService.queryShopAttributeValue(param);
           param.clear();
           param.put("shopProductType",shopProductType);
           param.put("attributeValueCount",attributeValueCount);
           param.put("shopProductAttribute",shopProductAttribute);
           param.put("attributeValueList", shopProductAttributeValueList);
           return ResultMsg.create(true,null,FastJsonUtils.toJSONString(param));
        }
        return ResultMsg.create(false);
    }

    /**
     * 删除商品属性（只能删除商品属性下没有属性值的数据）
     * @param productAttrId
     * @return
     */
    @RequestMapping(value="deleteProductAttribute",method=RequestMethod.POST)
    @ResponseBody
    public ResultMsg deleteProductAttribute(String productAttrId){
        if(Utils.objectIsNotEmpty(productAttrId)){
            Map<String,Object> param = new HashMap<>();
            param.put("productAttrId",productAttrId);
            int countShopAttributeValue = shopAttributeValueService.countShopAttributeValue(param);
            if(countShopAttributeValue==0){
                shopProductAttributeService.deleteShopProductAttribute(Integer.parseInt(productAttrId));
                return ResultMsg.create(true);
            }
        }
        return ResultMsg.create(false);
    }

    /**
     * 保存或修改商品属性
     * @param shopProductAttribute
     * @return
     */
    @RequestMapping(value="saveOrUpdateProductAttr",method=RequestMethod.POST)
    @ResponseBody
    public ResultMsg saveOrUpdateProductAttr(ShopProductAttribute shopProductAttribute){
        if(Utils.objectIsNotEmpty(shopProductAttribute)){
            if(Utils.objectIsEmpty(shopProductAttribute.getProductAttrId())){
                shopProductAttributeService.saveShopProductAttribute(shopProductAttribute);
            }else {
                shopProductAttributeService.updateShopProductAttribute(shopProductAttribute);
            }
            return ResultMsg.create(true);
        }else {
            return ResultMsg.create(false);
        }
    }

    /**
     * 移除商品属性值
     * @param attributeValueId
     * @return
     */
    @RequestMapping(value = "deleteAttributeValues",method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg deleteAttributeValues(Integer attributeValueId){
        if(Utils.objectIsNotEmpty(attributeValueId)){
            shopAttributeValueService.deleteShopAttributeValue(attributeValueId);
            return ResultMsg.create(true);
        }
        return ResultMsg.create(false);
    }

    /**
     * 检测商品属性名
     * @param name
     * @param productTypeId
     * @return
     */
    @RequestMapping(value = "checkName",method = RequestMethod.POST)
    @ResponseBody
    public Map checkName(String name,String productTypeId,Integer productAttrId){
        Map<String, Object> param = new HashMap<>();
        boolean flag=false;
        if (Utils.objectIsNotEmpty(name)) {
            param.put("name", name.trim());
            param.put("productTypeId",productTypeId);
            int countProductAttributeName = shopProductAttributeService.countShopProductAttribute(param); //统计名字的个数
            if (countProductAttributeName > 0) {
                if(Utils.objectIsNotEmpty(productAttrId)){
                    if(shopProductAttributeService.loadShopProductAttribute(productAttrId).getName().equals(name)){ //区分当前属性
                        flag = true;
                    }
                }else {
                    flag = false;
                }
            }else {
                flag = true;
            }
        }
        param.clear();
        param.put("valid",flag);//bootstrap validator 远程校验需要返回{"valid",true}
        return param;
    }

}
